﻿Imports System.IO
Imports System.Drawing.Imaging

Public Class ExifExtractor

	Public File As String
	Event Finished()
   Dim WithEvents Worker As New System.ComponentModel.BackgroundWorker With {.WorkerReportsProgress = True}
	Public Tags As New ExifTags

	Sub Start()
		Worker.RunWorkerAsync()
	End Sub

   Private Sub WorkerExif_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles Worker.DoWork
		GetExifInfo()
	End Sub

	Private Sub Worker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles Worker.RunWorkerCompleted
		RaiseEvent Finished()
	End Sub

	Public Sub GetExifInfo()
		If Not LCase(File).EndsWith(".jpg") Then Return
		Dim Items = My.Computer.FileSystem.GetFiles(MainFolder, FileIO.SearchOption.SearchAllSubDirectories, File)
		Dim Path = Items.First
		Dim Stream As FileStream = System.IO.File.OpenRead(Path)
		Dim Image As Image = Image.FromStream(Stream, False, False)
		Tags.Read(Image)
		Stream.Close()
		Image.Dispose()
	End Sub

	ReadOnly Property LocalThumbName As String
		Get
			Return My.Computer.FileSystem.SpecialDirectories.Temp & "\" & File.Replace("\", ".").Replace(":", ".") & ".thumb"
		End Get
	End Property

	ReadOnly Property CentralThumbName As String
		Get
			Return ThumbFolder & File.Replace("\", ".").Replace(":", ".") & ".thumb"
		End Get
	End Property

End Class
